package com.banggood.upms.service.impl;

import com.banggood.silk.web.api.exception.SkbException;
import com.banggood.upms.bean.exception.ExceptionEnum;
import com.banggood.upms.bean.po.TreeInfo;
import com.banggood.upms.bean.vo.search.TreeInfoSearchCriteria;
import com.banggood.upms.dao.TreeInfoDao;
import com.banggood.upms.service.TreeInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @author XuChuangFeng
 */
@Service
@Transactional(rollbackFor = SkbException.class)
public class TreeInfoServiceImpl extends BaseServiceImpl<TreeInfo, TreeInfoDao> implements TreeInfoService {

    @Override
    @Autowired
    public void setDao(TreeInfoDao dao) {
        super.dao = dao;
    }

    @Override
    public boolean ifTreeTypeExists(String treeType) throws SkbException {
        if(StringUtils.isEmpty(treeType)) {
            throw new SkbException(ExceptionEnum.MISSING_TREE_TYPE.getCode(), ExceptionEnum.MISSING_TREE_TYPE.getMessage());
        }
        TreeInfoSearchCriteria criteria = new TreeInfoSearchCriteria();
        criteria.setTreeType(treeType);
        List<TreeInfo> treeInfos = dao.selectByCriteria(criteria);
        if(CollectionUtils.isEmpty(treeInfos)) {
            return false;
        }
        return true;
    }
}
